package com.wang.ssm.controller;


import com.github.pagehelper.PageInfo;
import com.wang.ssm.pojo.User;
import com.wang.ssm.pojo.vo.ExamVo;
import com.wang.ssm.pojo.vo.RoomVo;
import com.wang.ssm.pojo.vo.StudentVo;
import com.wang.ssm.pojo.vo.TeacherVo;
import com.wang.ssm.service.StuService;
import com.wang.ssm.util.Result;
import com.wang.ssm.util.StatusCode;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 王瑞杰
 * date 2023/10/18
 */
@RestController
@RequestMapping("/student")
@CrossOrigin
public class StuController {
    @Resource
    private StuService stuservice;

    @PostMapping("/login")
    public Result<User> login(String username, String password){
        return this.stuservice.login(username,password)==null
                ? new Result<>( StatusCode.ERROR,"登录失败",false)
                : new Result<>(StatusCode.OK,"登录成功",true,this.stuservice.login(username,password));

    }
    @GetMapping("/students")
    public Result<PageInfo<StudentVo>> students(
            @RequestParam(name="offset",defaultValue = "1")Integer offset,
            @RequestParam(name="pageSize",defaultValue = "3")Integer pageSize,
                                                String sname){
        return this.stuservice.findAll(offset,pageSize,sname)==null
                ? new Result<>( StatusCode.ERROR,"查询失败",false)
                : new Result<>(StatusCode.OK,"查询成功",true,this.stuservice.findAll(offset,pageSize,sname));

    }

    @GetMapping("/room")
    public Result<List<RoomVo>> rooms(){
        return this.stuservice.findroom().size()==0
                ? new Result<>( StatusCode.ERROR,"查询失败",false)
                : new Result<>(StatusCode.OK,"查询成功",true,this.stuservice.findroom());

    }
    @GetMapping("/teachers")
    public Result<PageInfo<TeacherVo>> teachers(@RequestParam(name = "offter",defaultValue = "1")Integer offter,@RequestParam(name = "count",defaultValue = "3")Integer count){
        System.err.println("offter:"+offter+"---------count:"+count);
        return this.stuservice.findtea(offter,count)==null
                ? new Result<>( StatusCode.ERROR,"查询失败",false)
                : new Result<>(StatusCode.OK,"查询成功",true,this.stuservice.findtea(offter,count));

    }
    @GetMapping("/exams")
    public Result<List<ExamVo>> exams(){
        return this.stuservice.findex().size()==0
                ? new Result<>( StatusCode.ERROR,"查询失败",false)
                : new Result<>(StatusCode.OK,"查询成功",true,this.stuservice.findex());

    }
    @PostMapping("/save")
    public Result save(@RequestBody StudentVo student){
        System.out.println(student);
        boolean flag=stuservice.save(student);
        return flag ? new Result<>(StatusCode.OK,"新增成功",true)
                :new Result<>(StatusCode.ERROR,"新增失败",true);
    }

    @DeleteMapping("/delete")
    public Result delete(String[] arr){
        return stuservice.delete(arr);
    }


}
